package com.ayoka.parenting.ui.group.index;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.CircleGroupBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.jincp.common.utils.UIUtils;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GroupIndexPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GroupIndexPresenter extends BasePresenterImpl<GroupIndexContract.View> implements GroupIndexContract.Presenter {

    private GroupIndexModel model;

    public GroupIndexPresenter() {
        model = new GroupIndexModel();
    }

    @Override
    public void getCircleGroupListData(boolean isShowWaitDialog, int pageNo, int pageSize) {
        if (isShowWaitDialog) {
            mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        }
        model.getCircleGroupListData(this, pageNo, pageSize, new SimpleRequestCallback<PagerBean<CircleGroupBean>>() {
            @Override
            public void onSuccess(PagerBean<CircleGroupBean> data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configCircleGroupListData(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
            }
        });
    }

    @Override
    public void joinGroup(final String groupId, int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.joinGroup(this, groupId, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onOperationCallback(true, groupId);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onOperationCallback(false, groupId);
            }
        });
    }
}
